package id.mankel.commons.security.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class ShiroRealm extends AuthorizingRealm {

//    @Autowired
//    private SysUserMapper sysUserMapper;
//
//    @Autowired
//    private SysRoleMapper sysRoleMapper;

    public ShiroRealm(CredentialsMatcher matcher) {
        super(matcher);
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        return null;
//        Object principals = token.getPrincipal();
//
//        if (null == principals) {
//            throw new UnknownAccountException();
//        }
//
//        Object credentials = token.getCredentials();
//        if (null == credentials) {
//            throw new IncorrectCredentialsException();
//        }
//
//        SysUser sysUser = this.sysUserMapper.selectByUsername((String) principals);
//        if (null == sysUser) {
//            throw new UnknownAccountException();
//        }
//
//        SecurityUtils.setUserId(sysUser.getUserId());
//
//        return new SimpleAuthenticationInfo(principals, sysUser.getPassword(), ByteSource.Util.bytes(principals), super.getName());
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//        Integer userId = SecurityUtils.getUserId();
//        Set<String> roles = this.sysRoleMapper.selectRoleNameByUserId(userId);
//        info.setRoles(roles);
//        if (roles.contains(Constants.DEFAULT_SUPER_ROLE)) {
//            info.setStringPermissions(Set.of(Permission.ALL));
//        } else {
//            this.sysRoleMapper.selectPermissionsByUserId(userId).forEach((path, permSet) -> {
//                info.addStringPermissions(permSet.parallelStream().map(perm -> path + Asciis.COLON.s + perm).collect(Collectors.toSet()));
//            });
//        }
//        return info;
    }

}
